package com.spbl.apache.shiro.config;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.LogoutFilter;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Description(登出管理)
 * author: Gao xueyong
 * Create at: 2020/6/13 11:58 上午
 */
public class ShiroLogoutFilter extends LogoutFilter {

    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        Subject subject = getSubject(request, response);
        //登出
        subject.logout();
        //清除httpsession信息
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpSession session = httpServletRequest.getSession();
        if (session.getAttribute("user") != null) {
            session.removeAttribute("user");
        }
        //获取登出后重定向到的地址
        String redirectUrl = getRedirectUrl(request, response, subject);
        //重定向
        issueRedirect(request, response, redirectUrl);
        return false;
    }
}
